package com.xdcplus.web.service.task.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.web.common.pojo.dto.task.JobTaskSwysFilterDTO;
import com.xdcplus.web.common.pojo.entity.task.JobTaskAlarmSetting;
import com.xdcplus.web.common.pojo.entity.task.JobTaskStaffing;
import com.xdcplus.web.common.pojo.entity.task.JobTaskSwys;
import com.xdcplus.web.common.pojo.query.task.JobTaskAlarmSettingQuery;
import com.xdcplus.web.common.pojo.query.task.JobTaskStaffingQuery;
import com.xdcplus.web.common.pojo.query.task.JobTaskSwysQuery;
import com.xdcplus.web.common.pojo.vo.task.JobTaskSwysVO;
import com.xdcplus.web.mapper.task.JobTaskAlarmSettingMapper;
import com.xdcplus.web.mapper.task.JobTaskStaffingMapper;
import com.xdcplus.web.mapper.task.JobTaskSwysMapper;
import com.xdcplus.web.service.task.JobTaskSwysService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 丝印岗位任务(JobTaskSwys)表服务实现类
 *
 * @author Richard.Li
 * @since 2024-06-19 09:11:05
 */
@Slf4j
@Service("jobTaskSwysService")
public class JobTaskSwysServiceImpl extends BaseServiceImpl<JobTaskSwysMapper, JobTaskSwys, JobTaskSwys, JobTaskSwysVO> implements JobTaskSwysService {

    @Autowired
    protected JobTaskSwysMapper jobTaskSwysMapper;

    @Autowired
    protected JobTaskAlarmSettingMapper jobTaskAlarmSettingMapper;

    @Autowired
    protected JobTaskStaffingMapper jobTaskStaffingMapper;

    private List<JobTaskSwys> queryJobTaskSwysList(JobTaskSwysFilterDTO jobTaskSwysFilterDTO) {
        JobTaskSwysQuery jobTaskSwysQuery = BeanUtil.copyProperties(jobTaskSwysFilterDTO, JobTaskSwysQuery.class);

        return jobTaskSwysMapper.queryJobTaskSwysByToolName(jobTaskSwysQuery);
    }


    @Override
    public PageVO<JobTaskSwysVO> queryJobTaskSwys(JobTaskSwysFilterDTO jobTaskSwysFilterDTO) {

        PageVO<JobTaskSwysVO> pageVO = new PageVO<>();

        if (jobTaskSwysFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(jobTaskSwysFilterDTO.getCurrentPage(), jobTaskSwysFilterDTO.getPageSize(),
                    "DESC", "STATUS DESC,ALARM_TIME");
        }

        List<JobTaskSwys> jobTaskSwysList = queryJobTaskSwysList(jobTaskSwysFilterDTO);

        PageInfo<JobTaskSwys> pageInfo = new PageInfo<>(jobTaskSwysList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(jobTaskSwysList));

        return pageVO;
    }

    @Override
    public List<JobTaskSwysVO> objectConversion(List<JobTaskSwys> sList) {
        if (CollectionUtil.isEmpty(sList)) {
            return new ArrayList<>();
        } else {
            List<JobTaskSwysVO> tList = new ArrayList<>();

            for (JobTaskSwys s : sList) {
                JobTaskSwysVO t = this.objectConversion(s);
                JobTaskAlarmSettingQuery jobTaskAlarmSettingQuery = new JobTaskAlarmSettingQuery();
                jobTaskAlarmSettingQuery.setAlarmCode(s.getAlarmCode());
                jobTaskAlarmSettingQuery.setLineCode(s.getLineCode());
                List<JobTaskAlarmSetting> jobTaskAlarmSettings = jobTaskAlarmSettingMapper.queryJobTaskAlarmSettingByLine(jobTaskAlarmSettingQuery);
                if (!CollectionUtils.isEmpty(jobTaskAlarmSettings)){
                    t.setToolName(jobTaskAlarmSettings.get(0).getToolName());
                }

                if (ObjectUtil.isNotNull(t.getRestoreTime())) {
                    t.setEffectTime((t.getRestoreTime().getTime() - t.getAlarmTime().getTime())/1000);
                }else {
                    t.setEffectTime((Timestamp.from(Instant.now()).getTime() - t.getAlarmTime().getTime())/1000);
                }

                JobTaskStaffingQuery jobTaskStaffingQuery = new JobTaskStaffingQuery();
                jobTaskStaffingQuery.setTaskType(s.getTaskType());
                jobTaskStaffingQuery.setLineCode(s.getLineCode());
                List<JobTaskStaffing> jobTaskStaffings = jobTaskStaffingMapper.queryJobTaskStaffing(jobTaskStaffingQuery);
                if (!CollectionUtils.isEmpty(jobTaskStaffings)) {
                    t.setUserName(jobTaskStaffings.stream()
                            .map(JobTaskStaffing::getUserName)
                            .collect(Collectors.joining(",")));
                }
                Optional.of(t).ifPresent(tList::add);
            }
            return tList;
        }
    }

}
